package com.d2.modules.system.dept.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.d2.common.domain.Result;
import com.d2.modules.system.dept.entity.Dept;
import com.d2.modules.system.dept.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @PostMapping("add")
    public Result<Void> add(@RequestBody Dept dept){
        deptService.add(dept);
        return Result.success();
    }

    @DeleteMapping("delete/{id}")
    public Result<Void> delete(@PathVariable Long id){
        deptService.deleteById(id);
        return Result.success();
    }

    @PutMapping("update")
    public Result<Void> update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

    @GetMapping("tree")
    public Result<List<Dept>> tree(Long id, @RequestParam(value = "exclude", defaultValue = "-1") Long exclude){
        return Result.success(deptService.tree(id, exclude));
    }

    @GetMapping("children")
    public Result<List<Dept>> children(Long id, @RequestParam(value = "exclude", defaultValue = "-1") Long exclude){
        Dept query = new Dept();
        query.setPid(id);
        LambdaQueryWrapper<Dept> qw = Wrappers.lambdaQuery(query);
        qw.notIn(Dept::getId, exclude);
        return Result.success(deptService.list(qw));
    }

    /**
     * 获取它的上级和兄弟
     */
    @GetMapping("updateTreeView")
    public Result<List<Dept>> updateTreeView(Long id){
        return Result.success(deptService.updateTreeView(id));
    }
}
